class Solution
{
public:
    int longestAlternatingSubarray(vector<int> &nums, int threshold)
    {
        int maxLen = 0;
        int n = nums.size();
        for (int i = 0; i < n; ++i)
        {
            for (int j = i; j < n; ++j)
            {
                int len = j - i + 1;
                if (len <= maxLen)
                {
                    continue;
                }
                if (nums[i] > threshold || (nums[i] & 1))
                {
                    continue;
                }
                bool valid = true;
                for (int k = i + 1; k <= j; ++k)
                {
                    if (nums[k] > threshold)
                    {
                        valid = false;
                        break;
                    }
                    if (!((nums[k] & 1) ^ (nums[k - 1] & 1)))
                    {
                        valid = false;
                        break;
                    }
                }
                if (!valid)
                {
                    continue;
                }
                maxLen = len;
            }
        }
        return maxLen;
    }
};